<?php
/**
 * Description of ProfileController for User model
 *
 * @author Samsung
 */
class ProfileController extends Controller {
    
    /**
     * @var object current primary model class
     */
    private $_model;
    
    public function init() {
        parent::init();
        
        if(!Yii::app()->user->isGuest){
            //$homeText=Yii::app()->user->isMerchant? 'merchant.page.breadcrumbs':'user.page.breadcrumbs';
            /*
            $homeText=Yii::app()->user->displayName;
            $this->breadcrumbs = array(
                Lang::t($homeText) => Yii::app()->user->returnUrl,
            );*/
        }
    }
        
    public function accessRules(){
        return array(
            array('allow',
                //'actions'=>array(),
                'users'=>array('@'),
            ),
            array('deny'),
        );
    }
    
    
    public function actions() {
        return array(
            'deleteimage'=>array(
                'class'=>'ext.actions.GalleryAction',
                'module' => 'user',
                'moduleId'=>Yii::app()->request->getParam('id'),
                'imageId'=>Yii::app()->request->getParam('imageId'),
                'action'=>'delete',
            ),
        );
    }
    
    public function actionUpdate() {
        $model = $this->loadModel();
        $model->scenario = 'userUpdate';
        $model->autoFill();
        
        $address=empty($model->address)?new Address:$model->address;
        $address->autoFill($model);
        $address->validatorList->add(CValidator::createValidator('required', $address, 'address_'));
        
        $this->performAjaxValidation($model, $address);

        $params = Yii::app()->request->getParam(get_class($model));
        $addressParams = Yii::app()->request->getParam(get_class($address));
        if ($params && $addressParams) {
            $model->attributes = $params;
            $model->image_ = CUploadedFile::getInstances($model, 'image_');
            $model->validate();
            $address->attributes=$addressParams;
            $address->validate();
            
            if (!$model->hasErrors() && !$address->hasErrors() && $model->updateRecord($address)) {
                Yii::app()->setLanguage($model->preferredLanguageCode);
                LanguageSelectorPortlet::clearPreferredLanguage();
                $this->setFlash(Option::FLASH_TYPE_SUCCESS, Lang::t('user.profile.update.flash.success'));
                $this->refresh();
            }
        }

        $this->render('updateForm', array(
            'user' => $model,
            'address'=>$address,
            'formConfig' => array(
                'id' => $this->formId,
                'enableAjaxValidation' => true,
                'enableClientValidation' => true,
                'clientOptions' => array(
                    'validateOnSubmit' => true,
                ),
                'htmlOptions' => array(
                    'enctype' => 'multipart/form-data',
                ),
                'focus' => array($model, 'firstName_'),
            ),
        ));
    }

    public function actionSettings(){
        $model=$this->loadModel();
        $model->scenario='userUpdateSettings';
        $model->autoFill();
        $this->performAjaxValidation($model);
        
        $params = Yii::app()->request->getParam(get_class($model));
        if($params){
            $model->attributes=$params;
            if ($model->updateSettings()) {
                Yii::app()->setLanguage($model->preferredLanguageCode);
                LanguageSelectorPortlet::clearPreferredLanguage();
                $this->setFlash('success', Lang::t('account.settings.update.flash.success'));
                $this->refresh();
            }
        }
        
        $this->render('updateSettingsForm', array(
            'user' => $model,
            'formConfig' => array(
                'id' => $this->formId,
                'enableAjaxValidation' => true,
                'enableClientValidation' => true,
                'clientOptions' => array(
                    'validateOnSubmit' => true,
                ),
                'focus' => array($model, 'language_'),
            ),
        ));
    }
    
    public function actionPassword() {

        $model = $this->loadModel();
        $model->scenario = 'userUpdatePassword';
        $this->performAjaxValidation($model);

        $params = Yii::app()->request->getParam(get_class($model));
        if ($params) {
            $model->attributes=$params;
            if ($model->updatePassword()) {
                $this->setFlash(Option::FLASH_TYPE_SUCCESS, Lang::t('user.password.update.flash.success'));
                $this->refresh();
            }
        }

        $this->render('passwordForm', array(
            'model' => $model,
            'formConfig' => array(
                'id' => $this->formId,
                'enableAjaxValidation' => true,
                'enableClientValidation' => true,
                'clientOptions' => array(
                    'validateOnSubmit' => true,
                ),
                'focus' => array($model, 'oldPassword_'),
            ),
        ));
    }
    
    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     */
    public function loadModel() {
        if ($this->_model === null) {
            if (!Yii::app()->user->isGuest) {
                $this->_model = User::model()->accessible()->findByPk((int) Yii::app()->user->id);
            }
            if ($this->_model === null)
                HttpStatus::code(404);
        }
        return $this->_model;
    }
    
}